import 'package:mall/utils/broadcast/broadcast_util.dart';

import '../../utils/sc/state_controller.dart';
import '../model/address_detail_entity.dart';
import '../model/address_entity.dart';
import '../service/mine_service.dart';

class AddressDetailSc extends ModelSC<AddressDetailEntity> {
  int? addressId;

  @override
  Future<AddressDetailEntity?> loadData() async {
    if (addressId == null) {
      throw ArgumentError('addressId 不能为空');
    }

    final res = MineService.queryAddressDetail(addressId!);
    return res;
  }

  setAddressArea(String areaId, String provinceName, String cityName, String countryName, String name, String phone,
      String addressDetail) {
    var model = data.value;
    if (model == null) {
      return;
    }
    model.areaCode = areaId;
    model.province = provinceName;
    model.city = cityName;
    model.county = countryName;
    model.name = name;
    model.tel = phone;
    model.addressDetail = addressDetail;
  }
}

class AddressListSc extends PagingListSC<AddressList> implements AddressEvent {
  @override
  void onInit() {
    super.onInit();
    BroadcastUtil.register<AddressEvent>(this);
  }

  @override
  Future<List<AddressList>> loadData() async {
    final res = await MineService.getAddressList();
    count = res.total;
    return res.list ?? [];
  }

  @override
  void addOrUpdateAddress() {
    refreshData();
  }

  @override
  void deleteAddress(int id) {
    refreshData();
  }
}
